﻿namespace Vboot.App.De;

//供应商服务
public class DeSuppMainService : BaseMainService<DeSuppMain>, ITransient
{
    
    //新增
    public async Task<string> Insertx(DeSuppMain main)
    {
        
        main.id = YitIdHelper.NextId() + "";
        main.crmid = XuserUtil.getUserId();
        await repo.Context.Insertable(main)
            .AddSubList(it => it.contacts.First().maiid)
            .AddSubList(it => it.atts.First().maiid)
            .ExecuteCommandAsync();
        var viewers = new List<DeSuppViewer>();
        foreach (var viewer in main.viewers)
        {
            viewers.Add(new DeSuppViewer {mid = main.id, oid = viewer.id});
        }
        var editors = new List<DeSuppEditor>();
        foreach (var editor in main.editors)
        {
            editors.Add(new DeSuppEditor {mid = main.id, oid = editor.id});
        }
        await repo.Context.Insertable(viewers).ExecuteCommandAsync();
        await repo.Context.Insertable(editors).ExecuteCommandAsync();
        return main.id;
    }

    //修改
    public async Task<string> Updatex(DeSuppMain main)
    {
        main.upmid = XuserUtil.getUserId();
        var viewers = new List<DeSuppViewer>();
        if (main.viewers != null)
        {
            foreach (var viewer in main.viewers)
            {
                viewers.Add(new DeSuppViewer {mid = main.id, oid = viewer.id});
            }
        }

        var editors = new List<DeSuppEditor>();
        if (main.editors != null)
        {
            foreach (var editor in main.editors)
            {
                editors.Add(new DeSuppEditor {mid = main.id, oid = editor.id});
            }
        }
        await base.UpdateAsync(main);
        await repo.Context.Deleteable<DeSuppViewer>().Where(it => it.mid == main.id).ExecuteCommandAsync();
        await repo.Context.Deleteable<DeSuppEditor>().Where(it => it.mid == main.id).ExecuteCommandAsync();
        await repo.Context.Insertable(viewers).ExecuteCommandAsync();
        await repo.Context.Insertable(editors).ExecuteCommandAsync();
        
        
        //比对数据库中的contact,插入页面上新增的，更新页面上修改的，删除页面上删除的
        List<DeSuppContact> list = await repo.Context.Queryable<DeSuppContact>()
            .Where(t => t.maiid == main.id).ToListAsync();
        List<DeSuppContact> insertList = new List<DeSuppContact>();
        List<DeSuppContact> updateList = new List<DeSuppContact>();
        List<DeSuppContact> deleteList = new List<DeSuppContact>();
        for (int i = 0; i < list.Count; i++)
        {
            bool flag = false;
            for (int j = 0; j < main.contacts.Count; j++)
            {
                if (main.contacts[j].id == list[i].id)
                {
                    flag = true;
                    updateList.Add(main.contacts[j]); //找到相同的则更新
                    break;
                }
            }
        
            if (!flag)
            {
                deleteList.Add(list[i]);
            }
        }
        
        for (int j = 0; j < main.contacts.Count; j++)
        {
            bool flag = false;
            for (int i = 0; i < list.Count; i++)
            {
                if (main.contacts[j].id == list[i].id)
                {
                    flag = true;
                    break;
                }
            }
        
            if (!flag)
            {
                insertList.Add(main.contacts[j]);
            }
        }
        
        await repo.Context.Deleteable(deleteList).ExecuteCommandAsync();
        await repo.Context.Updateable(updateList).ExecuteCommandAsync();
        await repo.Context.Insertable(insertList).ExecuteCommandAsync();
        
        
        //比对数据库中的att,插入页面上新增的，更新页面上修改的，删除页面上删除的
        List<DeSuppAtt> attList = await repo.Context.Queryable<DeSuppAtt>()
            .Where(t => t.maiid == main.id).ToListAsync();
        List<DeSuppAtt> attInsertList = new List<DeSuppAtt>();
        List<DeSuppAtt> attUpdateList = new List<DeSuppAtt>();
        List<DeSuppAtt> attDeleteList = new List<DeSuppAtt>();
        for (int i = 0; i < attList.Count; i++)
        {
            bool flag = false;
            for (int j = 0; j < main.atts.Count; j++)
            {
                if (main.atts[j].id == attList[i].id)
                {
                    flag = true;
                    attUpdateList.Add(main.atts[j]); //找到相同的则更新
                    break;
                }
            }

            if (!flag)
            {
                attDeleteList.Add(attList[i]);
            }
        }

        for (int j = 0; j < main.atts.Count; j++)
        {
            bool flag = false;
            for (int i = 0; i < attList.Count; i++)
            {
                if (main.atts[j].id == attList[i].id)
                {
                    flag = true;
                    break;
                }
            }

            if (!flag)
            {
                attInsertList.Add(main.atts[j]);
            }
        }

        await repo.Context.Deleteable(attDeleteList).ExecuteCommandAsync();
        await repo.Context.Updateable(attUpdateList).ExecuteCommandAsync();
        await repo.Context.Insertable(attInsertList).ExecuteCommandAsync();
        
        return main.id;
    }

    //删除
    public async Task Deletex(string ids)
    {
        var idArr = ids.Split(",");
        await repo.Context.Deleteable<DeSuppMain>().In(idArr).ExecuteCommandAsync();
        for (int i = 0; i < idArr.Length; i++)
        {
            var mid = idArr[i];
            await repo.Context.Deleteable<DeSuppViewer>().Where(it => it.mid == mid).ExecuteCommandAsync();
            await repo.Context.Deleteable<DeSuppEditor>().Where(it => it.mid == mid).ExecuteCommandAsync();
            await repo.Context.Deleteable<DeSuppContact>().Where(it => it.maiid == mid).ExecuteCommandAsync();
            await repo.Context.Deleteable<DeSuppAtt>().Where(it => it.maiid == mid).ExecuteCommandAsync();
        }
    }

    public DeSuppMainService(SqlSugarRepository<DeSuppMain> repo)
    {
        this.repo = repo;
    }
}